import {
	lesson_coach,
	lesson_athlete,
	athletesList,
	coachList,
	participant
} from '@/api/train'
import $router from '@/router';
const state = {
	lessonId: '', //课程id
	//日程中的教练rollCall:0/运动员rollCall:1的 list
	lessonCoachList: [],
	rollCall: 0,
	lessonAthleteList: [],
	athleteCoachList: [],
	ParticipantAthlete: {},
	ParticipantAthlete_titleNameList: [{
		value: 0,
		text: 0
	}, ],
	ParticipantAthleteList: [],
	queryAthletesList: [],
	queryCoachList: [],
	//保存运动员签到信息
	//athleteRollCall 参数说明
	//  [{
	// "id": 41, "点名id"
	// "reason": "生病了", "理由"
	// "signCoordinate": "546546,21454", “坐标”
	// "signInTime": "", “签到时间”
	// "signLocation": "天河区东风路", “签到地址”
	// "status": "ABSENT"“状态 CHECK-IN=正常 LAST_EARLYLEAVE=迟到或早退，ABSENT=缺席“
	// }]
	resultAthleteCoach:{lesson:{}},
	athleteRollParams: {
		athleteRollCall: [],
		lessonId: ''
	},
	ParticipantParams: {
		participant: [], //要删除的参与者id json列表字符串 eg:[1]
		lessonId: '',
		uts: '', //	获取到的课程的uts
		removeIds: [],
	},
	//participant参数说明 update
	// [{
	// "id": 51,//参与者id 如果是添加的为null
	// "masterCoach": 1, 是否为主教练
	// "rateCardTitle": 0, 结算的cardid
	// "rollCall": 0, 是否为帮手
	// "userId": 91,用户id
	// }] 

}

const mutations = {
	GET_LESSON_COACH: (state, info) => {
		state.rollCall = info.rollCall === 'YES'
		state.lessonCoachList = info.coachList //inofo=>日程教练
		state.athleteCoachList = info.coachList
		state.resultAthleteCoach.lesson = info.lesson
		console.log('infoinfo', info)
	},
	GET_LESSON_ATHLETE: (state, info) => {
		state.rollCall = info.rollCall === 'YES'
		state.athleteCoachList = info.athleteList
		state.resultAthleteCoach.lesson = info.lesson
		state.lessonAthleteList = info.athleteList //lessonId=>日程运动员
		console.log('infoinfo', info, state)
	},
	PUSH_ATHLETE_PARAMS: (state, info) => {
		console.log('athleteRollCall', info)
		state.athleteRollParams = info //lessonId=>日程运动员
	},
	ParticipantAthleteList: (state, info) => {
		console.log('ParticipantAthleteList', info)
		state.ParticipantAthleteList = info //lessonId=>日程运动员
	},
	//  ATHLETE_CHECK: (state, info ) => {
	// console.log('athleteRollCall',info)
	// state.athleteRollParams = info //lessonId=>日程运动员
	//  }
	queryAthletesList: (state, info) => {
		console.log('athleteRollCall', info, this)
		state.queryAthletesList = info //教练
	},
	queryCoachList: (state, info) => {
		console.log('athleteRollCall', info)
		state.queryCoachList = info //运动员
	}

}

const actions = {
	lessonId(obj) {
		return obj
	},
	//日程教练
	lessonCoachList({
		commit,
		state
	}, info) {
		console.log('lessonCoachList', state, info)
		this._vm.$ajax.post('/roll_call/query/lesson_coach', {
			params: info
		}, (response) => {
			const data = response
			//console.log(21312,response.status===0,lesson_coach)

			if (response.status === 0) {
				state.athleteRollParams.athleteRollCall=[]
				data.result.coachList.map((maplis) => {
					maplis._reason = maplis.reason
					maplis.reason_ = maplis.reason
					maplis._workingHour = maplis.workingHour
					maplis.workingHour_ = maplis.workingHour
					return maplis
				})
				commit('GET_LESSON_COACH', data.result)
			}
		}, () => {
			//commit('GET_LESSON_COACH', lesson_coach.result)
		})
	},
	ParticipantAthleteList({
		state,
		commit
	}, params) {
		console.log(state, params)
		this._vm.$ajax.post('/roll_call/view/participant', {
			params
		}, (response) => {
			const data = response.result
			state.ParticipantAthlete = data
			state.ParticipantAthlete_titleNameList = []
			if (state.ParticipantAthlete.titleNameList) {
				state.ParticipantAthlete.titleNameList.map(list => {
					var b = {
						text: list.titleName,
						value: list.id,
						rateType: list.list
					}
					state.ParticipantAthlete_titleNameList.push(b)
				})
			}
			commit('ParticipantAthleteList', data.participantList)
		}, () => {

			//commit('ParticipantAthleteList',data.participantList)
			//state.ParticipantAthleteList = participant.result.participantList
			//commit('GET_LESSON_ATHLETE', lesson_athlete.result)
		})
	},
	ParticipantParams({
		state
	}, info) {
		//console.log('ParticipantParams',commit,state, info)
		if (info.participant) {

			if (info.participant.length) {
				state.ParticipantParams.participant.map(paramList => {
					if (info.participant.id == paramList.id) {
						paramList = info.participant
					} else {
						state.ParticipantParams.participant.push(info.participant)
					}
					return paramList
				})
			} else {
				state.ParticipantParams.participant.push(info.participant)
			}
		}
		if (!state.lessonId && info.lessonId) state.ParticipantParams.lessonId = info.lessonId
	},
	//更新 助手/教練和運動員
	updateParticipant({
		state
	}, info) {

		let ParticipantParams = state.ParticipantParams
		ParticipantParams.participant = []
		state.ParticipantAthleteList.map(list => {
			var pant = info.type === 'athlete' ? list.userId : {
				id: list.id,
				masterCoach: list.masterCoach,
				rollCall: list.rollCall,
				rateCardTitle: list.rateCardTitle,
				userId: list.userId
			}

			ParticipantParams.participant.push(pant)
		})

		ParticipantParams.lessonId = info.lessonId
		ParticipantParams.uts = info.uts
		//var that =this._vm
		this._vm.$ajax({
			url: '/roll_call/update/lesson/' + info.type || 'athlete',
			method: 'post',
			data: state.ParticipantParams
		}).then(() => {
			$router.replace({
				path: '/train/details',
				query: {
					lessonId: info.lessonId
				}
			})
		}).catch(() => {

		})

		//$router.push({path:'/train/edit',query:{lessonId:info.lessonId}})


	},
	//日程运动员
	lessonAthleteList({
		commit,
		state
	}, info) {
		console.log(state, info)
		this._vm.$ajax.post('/roll_call/query/lesson_athlete', {
			params: { ...info
			}
		}, (response) => {
			const data = response
			console.log(21312, response.status === 0, lesson_athlete)
			if (response.status === 0) {
				state.athleteRollParams.athleteRollCall=[]
				data.result.athleteList.map((maplis) => {
					maplis._reason = maplis.reason
					maplis.reason_ = maplis.reason
					maplis._workingHour = maplis.workingHour
					maplis.workingHour_ = maplis.workingHour
					return maplis
				})
				commit('GET_LESSON_ATHLETE', data.result)
			}
			// }else{
			// commit('GET_LESSON_ATHLETE', lesson_athlete.result)
			// }
		}, () => {
			//commit('GET_LESSON_ATHLETE', lesson_athlete.result)
		})
	},
	queryAthletesList({
		commit,

	}, params) {
		this._vm.$ajax.get('/roll_call/search/user', {
				params
			},
			(res) => {
				const data = res.result
				let result = [],
					vm = this._vm
				data.map(list => {
					result.push({
						text: vm.filterName(list),
						value: list
					})
				})

				commit('queryAthletesList', result)
			}, () => {
				// 	let result=[],vm=this._vm
				// 	athletesList.result.map(list=>{
				// 	result.push({text:vm.filterName(list),value:list})
				// 	})
				// commit('queryAthletesList',result)
			})

	},
	queryCoachList({
		commit

	}, params) {
		this._vm.$ajax.get('/roll_call/search/user', {
			params
		}, (res) => {
			const data = res.result
			let result = [],
				vm = this._vm
			data.map(list => {
				result.push({
					text: vm.filterName(list),
					value: list
				})
			})
			commit('queryCoachList', result)
		}, () => {
			// 	let result=[],vm=this._vm
			// 	coachList.result.map(list=>{
			// 	result.push({text:vm.filterName(list),value:list})
			// 	})
			// commit('queryCoachList',result)
		})
	},
	pushqueryAthletesList({
		state
	}, info) {
		console.log('pushqueryAthletesList', info)
		state.ParticipantAthleteList.push(info) //教练
	},
	pushqueryCoachList({
		state
	}, info) {
		console.log('pushqueryCoachList', state, info)
		state.ParticipantAthleteList.push(info) //运动员
	},

	athleteRollParams({
		commit,
		state
	}, info) {
		//debugger
		//console.log('athleteRollParams',commit,state, info)
		info.athleteRollCall?info.athleteRollCall.signCoordinate = this.getters.coordinate:''
		this._vm.env.NODE_ENV === 'development'&&this.getters.coordinate?alert(this.getters.coordinate):"";
		if (info.athleteRollCall) {
			if (state.athleteRollParams.athleteRollCall.length) {
				state.athleteRollParams.athleteRollCall.map((paramList,i) => {
					if (info.athleteRollCall.id == paramList.id) {
						return state.athleteRollParams.athleteRollCall[i] = info.athleteRollCall
					}else{
						state.athleteRollParams.athleteRollCall.push(info.athleteRollCall)
						return paramList
					}
					
				})
			} else {
				state.athleteRollParams.athleteRollCall.push(info.athleteRollCall)
			}
		}

		if (!state.lessonId && info.lessonId) state.athleteRollParams.lessonId = info.lessonId
		if (info) {
			commit('PUSH_ATHLETE_PARAMS', state.athleteRollParams)
		} else {
			console.error('athleteRollParams', state.athleteRollParams, state, info)
		}
	},
	//手动签到保存 运动员、教练，助手
	athleteCheck({
		commit,
		state
	}, {
		data,
		athlete,
		_this
	}) {
		console.log('athleteCheck', commit, state, data)
		let that = this
		this._vm.$ajax({
			method: 'post',
			url: '/roll_call/' + athlete + '/check',
			data: {
				lessonId: data.lessonId,
				[athlete + 'RollCall']: data.athleteRollCall
			}
		}).then(response => {
			//禪道 4215

			that._vm.$toast.success(_this.$t('app.Common.rightText'))
			$router.replace({
				path: '/train/details',
				query: {
					lessonId: data.lessonId
				}
			})

			console.log('athleteCheck sc', response.status === 0, lesson_athlete)
		})
	}

}

export default {
	namespaced: true,
	state,
	mutations,
	actions
}
